//
//  KKWelcomeViewController.swift
//  SinaWeiBo
//
//  Created by King.Kong. on 16/5/25.
//  Copyright © 2016年 King.Kong. All rights reserved.
//

import UIKit
import SDWebImage

class KKWelcomeViewController: UIViewController {

    override func loadView() {
        view = backView
    }
    override func viewDidLoad() {
        super.viewDidLoad()

       setupUI()
    }
    
    private func setupUI() {
        
        view.addSubview(iconView)
        view.addSubview(nameLable)
        nameLable.alpha = 0
        
        // 设置约束
        iconView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-160)
        }
        
        nameLable.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
        
//        startAnimation()
        
        // 切圆角
        iconView.cornerRadius = 30
        
        // 加载网络图片
        iconView.sd_setImageWithURL(KKUserAccountViewModel.sharedAccountViewModel.headURL)
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    // 动画
    private func startAnimation() {
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 6, options: [], animations: { 
            self.iconView.snp_updateConstraints(closure: { (make) in
                make.bottom.equalTo(self.view).offset(160 - ScreenHeight)
            })
            // 更改约束 收集 提前刷新视图
            self.view.layoutIfNeeded()
            }) { (_) in
                
         UIView.animateWithDuration(0.5, animations: { 
            self.nameLable.alpha = 1
            }, completion: { (_) in
                
                /// 切换根控制器
                NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: "main")
         })
        }
        
    }
    
    // 隐藏状态栏
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    /// 懒加载控件
    private lazy var backView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))

    private lazy var nameLable:UILabel = UILabel(title:KKUserAccountViewModel.sharedAccountViewModel.userAccount?.name ?? "", textColor: UIColor.darkGrayColor(), font:18)
        
        
    
//        {
//        
//        let l = UILabel()
//        
//        l.text = KKUserAccountViewModel.sharedAccountViewModel.userAccount?.name
//        l.textColor = UIColor.darkGrayColor()
//        l.font = UIFont.systemFontOfSize(18)
//        l.sizeToFit()
//        l.alpha = 0
//        
//        return l
//    }()
 }
